<?php
session_start();
require_once 'include/db.php';
require_once 'include/userClass.php';
require_once 'include/postClass.php';
$post = new postClass ($user -> getUserID(), $db);

if (isset ($_POST['post']) && $_POST['postTitle']) {
	$newPost = $post -> insertPost($_POST['postTitle'], $_POST['post'], $user -> userName);
	header("Location: showPost.php?id={$newPost}");

}
$title = "Nytt innlegg"; 
include 'include/header.php';
if($user -> userLoggedOn()){
?>
	<div id="content">
		<h2>Nytt innlegg</h2>
		<form class="post" method="post" action="newPost.php">
	 <!-- Post title  -->
		<label for="postTitle">Title</label>
		<input type="text" name="postTitle" required/>
		<br/>
	 <!-- The actual post  -->
	    <textarea name ="post"></textarea>
	    <input type ="submit" value="Lagre Innlegg"></input>
	    </form>
	 <!-- Send user back to users profile page -->
	   <form class="postback" method="get" action="displayUsr.php">
		<input type="hidden" name="usrName" value="<?php echo $user->userName ?>">
	    <input type="submit" value="Tilbake">
	   </form>
	</div>
	<div id="side">
	<?php
		if($user -> userLoggedOn()){
			$user -> showUserInfo($user -> getUserInfo(NULL));
			if($user->isUserAdmin() == true) {
				echo "<a class='button' href='adminStat.php?usrName=".$user->userName."' title='Statistikk'>Statistikk</a><br/>\n
				<a class='button' href='newAdmin.php?usrName=".$user->userName."' title='Ny admin'>Ny administrator</a><br/>
				<a class='button' href='displayUsr.php?usrName=".$user->userName."&own=true' title='Min blogg'>Min blogg</a><br/>";
			}
			echo "<a class='button' href='newPost.php?usrName=".$user->userName."' title='Nytt innlegg'><img class='icon' src='include/img/Edit.png' />Nytt innlegg</a><br/>\n
			<a class='button' href='editUser.php' title='Rediger profil'><img class='icon' src='include/img/Wheel.png' />Rediger profil</a>";
	}
	?>
	</div>
<?php }
 include 'include/footer.php';?>